import android.content.Context
import com.swedne.test.R
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.withContext
import java.io.File
import java.util.Locale
import kotlin.system.measureTimeMillis

fun main(args: Array<String>) {
//    testFor()
//    filterList()
    //testVerify()
//    timeTest()
    test()
}

/*变量*/
var str = "1"
var a = 1
val a1 = 2

/*函数*/
fun add(a: Int, b: Int): Int {
    return a + b
}

fun add2(a: Int, b: Int = 3) = a + b

/*其他*/
fun testWhen(a: Int) {
    when (a) {
        1 -> println(1)
        2 -> println(2)
        3, 4 -> println("a>2")
        else -> {

        }
    }
}

fun testFor() {
    for (i in 1..5) print(i)
    println()
    for (i in 5 downTo 1) print(i)
    println()
    for (i in 10 downTo 0 step 2) print(i)
}
/*空安全*/

//Kotlin 的类型系统将类型区分为 nullable 和 non-nullable 两种类型：
var d: String? = null
var d2: String = ""
fun nullDeal(string: String?) {
    d = null
    println("str length=${string?.length ?: 0}")
}

/*智能类型转换*/

var e: Int = 1
var e1 = 1

fun testTypeTransfer(x: Any) {
    if (x is String) {
        x.length
    }
}

fun handleInput(input: Any) {
    when (input) {
        is String -> println("String with length ${input.length}")
        is Int -> println("Integer with value ${input + 1}")
        else -> println("Unknown type")
    }
}

/*扩展函数*/

var context: Context? = null

fun Int.str() =
    com.swedne.test.demo.context?.getString(this) ?: ""

/*扩展属数*/
val <T> List<T>.lastIndex: Int
    get() = this.size - 1

var f = 1
    set(value) {
        field = value
    }
    get() = if (field > 10) 10 else field


/*高阶函数特性*/
fun <T> List<T>.customFilter(predicate: (T) -> Boolean): List<T> {
    val result = mutableListOf<T>()
    for (item in this) {
        if (predicate(item)) {
            result.add(item)
        }
    }
    return result
}

fun filterList() {
    listOf(1, 2, 3, 4, 5, 6, 7, 8).run {
        //没使用lambda,可读性和复用性
        customFilter(::isEven)

        //使用lambda,简介灵活,快速定义
        customFilter { it % 2 == 0 }.forEach {
            println(it)
        }
    }
}


fun isEven(i: Int) = i % 2 == 0

fun modifyString(str: String, operation: (String) -> String): String {
    return operation(str)
}

fun testFun() {
    modifyString("ABDCD") { x -> x.toLowerCase(Locale.ROOT) }
}

//接受函数返回

// 定义一个高阶函数，它接受一个操作函数作为参数，并返回一个带有计时功能的函数
fun <T>timedOperation(operation: () -> T?): () -> T? {
    return {
        println("Operation start")

        var result: T? = null
        val timeTaken = measureTimeMillis {
            result = operation() // 执行操作并存储结果
        }
        println("Operation end")
        println("Operation timeTaken = $timeTaken ms")

        result // 返回结果
    }
}

fun requestData(): String {
    Thread.sleep(1000)
    return "jsonStr"
}

fun timeTest() {
    val timedOperation = timedOperation(::requestData)
    println(timedOperation())

}

/*协程*/
 fun test(){
    print("========1")
    CoroutineScope(Dispatchers.IO).async {
        Thread.sleep(5000)
        print("dfsfsdfsdfsdf")
        withContext(Dispatchers.Main){
            print("132123")
        }
    }
    print("========2")
}

val sdfdf:(String)->Int={it-> it.length}
val sdfdf2={it:String-> "${it}////"}














